"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '11/05/2025'
    comment: 1v1视听会议
"""
from fastapi import APIRouter, Request, Body, Path, Query

from project.model.audio_video import ApiRoom, Room, ApiToken, ApiRecording, ApiRecordingFiles
from project.api import api_handler, ApiResponse
from project.service.audio_video import AudioService
from project.middleware.zego import Zego


audio_v1_router = APIRouter(prefix='/api/v1', tags=['V1'])


@audio_v1_router.post('/rooms', summary='创建房间', response_model=ApiRoom)
@api_handler()
async def create_rooms(request: Request, room_name: str = Body(..., title='房间名称', embed=True)):
    """
    **desc**: 创建房间

    **url**: /api/v1/rooms

    **method**: POST

    **params**:

        room_name: 房间名称

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:{
                id: 房间id
                code: 房间号
                name: 房间名称
                user_id: 用户id
                is_stop: 是否停止
            }
        }
    """
    room = AudioService.create_room(room_name=room_name)

    return ApiRoom.success(data=room.model_dump())


@audio_v1_router.get('/token', summary='获取token', response_model=ApiToken)
@api_handler()
async def get_tokens(request: Request,
                     room_code: str = Query(..., title='房间号'),
                     user_id: str = Query(..., title='用户id', description='创建房间/加入房间返回')):
    """
    **desc**: 取得RTC、RTM token

    **url**: /api/v1/token

    **method**: GET

    **params**:

        room_code: 房间名称
        user_id: 用户id

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:{
                token:
            }
        }
    """
    token = Zego.generate_token(
            user_id=user_id,
            effective_time_in_seconds=3600,
            payload={
                'room_code': room_code,
                'privilege': {1: 1, 2: 1}
            }
        )

    return ApiToken.success(data={'token': token})


@audio_v1_router.post('/rooms/{room_code}/join', summary='加入房间', response_model=ApiRoom)
@api_handler()
async def join_room(request: Request, room_code: int = Path(..., title='房间号')):
    """
    **desc**: 加入房间

    **url**: /api/v1/rooms/{room_code}/join

    **method**: POST

        <path>:
            room_code: 房间号

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: None
        }
    """
    room = Room.join(room_code=room_code)

    return ApiRoom.success(data=room.model_dump())


@audio_v1_router.post('/rooms/{room_code}/record/start', summary='开始录制', response_model=ApiRecording)
@api_handler()
async def start_recording(request: Request, room_code: int = Path(..., title='房间号')):
    """
    **desc**: 开始录制

    **url**: /api/v1/rooms/{room_code}/record/start

    **method**: POST

        <path>:
            room_code: 房间号

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                recording_id:
            }
        }
    """
    recording_id = AudioService.start_recording(room_code)

    return ApiRecording.success(data={'recording_id': recording_id})


@audio_v1_router.post('/rooms/{room_code}/record/stop', summary='停止录制', response_model=ApiResponse)
@api_handler()
async def stop_recording(request: Request, room_code: int = Path(..., title='房间号')):
    """
    **desc**: 停止录制

    **url**: /api/v1/rooms/{room_code}/record/stop

    **method**: POST

        <path>:
            room_code: 房间号

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: None
        }
    """
    AudioService.stop_recording(room_code)

    return ApiResponse.success(data=None)


@audio_v1_router.get('/recordings/{room_code}', summary='获取录制视频', response_model=ApiRecordingFiles)
@api_handler()
async def get_recording_files(request: Request, room_code: str = Path(..., title='房间号')):
    """
    **desc**: 获取录制文件

    **url**: /api/v1/recordings/{recording_id}

    **method**: GET

    **params**:

        <path>:
            room_code: 房间号

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                files: [
                    {
                        name: 文件名
                        last_time: 最后更新时间
                        url: 下载链接
                    }
                ]
            }
        }
    """
    files = AudioService.get_recording_files(room_code=room_code)

    return ApiRecordingFiles.success(data={'files': files})
